package com.zysc.processflow.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.zysc.processflow.common.util.StringUtils;
import com.zysc.processflow.entity.*;
import com.zysc.processflow.mapper.HandleprocessMapper;
import com.zysc.processflow.mapper.HandleprocessnodeMapper;
import com.zysc.processflow.pojo.ProcessConfig;
import com.zysc.processflow.service.*;
import com.zysc.processflow.common.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 处置流程节点表 服务实现类
 * </p>
 *
 * @author 正元智慧城市_
 * @since 2022-11-18
 */
@Service
public class HandleprocessnodeServiceImpl extends BaseService<HandleprocessnodeMapper, Handleprocessnode> implements IHandleprocessnodeService {
    @Autowired
    IOrganizationService organizationService;
    @Autowired
    IPersonService personService;
    @Autowired
    HandleprocessMapper handleprocessMapper;
    /**
     * 根据流程ID获取流程节点配置信息
     * @param processId 流程ID
     * @return 流程节点配置信息列表
     */
    @Override
    public List<ProcessConfig.NodeConfig> getNodesConfigByProcessId(String processId) {
        List<ProcessConfig.NodeConfig> result = new ArrayList<>();

        // 判断流程ID是否为空
        if (!StringUtils.isNull(processId)) {
            LambdaQueryWrapper<Handleprocessnode> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Handleprocessnode::getProcessid, processId);

            // 查询流程节点列表
            List<Handleprocessnode> list = this.list(queryWrapper);

            Handleprocess handleprocess = handleprocessMapper.selectById(processId);
            // 遍历节点列表
            for (Handleprocessnode node : list) {

                String oldConfig = handleprocess.getConfig();
                ProcessConfig.NodeConfig nodeConfig = new ProcessConfig.NodeConfig();

                // 设置节点ID、节点名称、节点类型、节点业务类型、角色ID、角色名称、是否需要发送消息
                nodeConfig.setId(node.getId());
                if(StringUtils.isNull(node.getNodename())){
                    nodeConfig.setName(node.getId());
                }else{
                    nodeConfig.setName(node.getNodename());
                }
                nodeConfig.setType(node.getNodetype());
                nodeConfig.setBusinesstype(node.getType());
                nodeConfig.setRoleid(node.getRoleid());
                nodeConfig.setCustom(node.getCustom());
                nodeConfig.setRolename(node.getRolename());
                nodeConfig.setNeedsendmsg(node.getNeedsendmsg());
                nodeConfig.setIco("Coin");

                try{
                    // 设置组织信息
                    if(!StringUtils.isNull(node.getOrgid())){
                        List<Organization> orgList = new ArrayList<>();
                        LambdaQueryWrapper<Organization> orgQueryWrapper = new LambdaQueryWrapper<>();
                        orgQueryWrapper.eq(Organization::getId, node.getOrgid());
                        orgList = organizationService.list(orgQueryWrapper);
                        nodeConfig.setOrginfo(orgList);
                    }else{
                        nodeConfig.setOrginfo(new ArrayList<>());
                    }
                }catch(Exception e){}

                try{
                    // 设置处理者信息
                    if(!StringUtils.isNull(node.getDisposerid())){
                        List<Person> perList = new ArrayList<>();
                        LambdaQueryWrapper<Person> perQueryWrapper = new LambdaQueryWrapper<>();
                        perQueryWrapper.eq(Person::getId, node.getDisposerid());
                        perList = personService.list(perQueryWrapper);
                        nodeConfig.setPerinfo(perList);
                    }else{
                        nodeConfig.setPerinfo(new ArrayList<>());
                    }
                }catch(Exception e){}

                try{
                    // 设置节点的左右位置和图标
                    JSONArray nodeList = JSON.parseObject(oldConfig).getJSONArray("nodeList");
                    nodeList.stream().forEach(item -> {
                        Map mapitem = (Map) item;
                        if (mapitem.get("id").equals(node.getId())) {
                            nodeConfig.setLeft((String)mapitem.get("left"));
                            nodeConfig.setTop((String)mapitem.get("top"));
                            nodeConfig.setIco((String)mapitem.get("ico"));
                            nodeConfig.setState((String)mapitem.get("state"));
                        }
                    });
                }catch(Exception e){}

                result.add(nodeConfig);
            }
        }

        return result;
    }


}
